package top.infopub.mgr.mkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.BidSecurityDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.dao.BidSecurityDtoDao;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.service.BidSecurityService;
import top.infopub.model.mkt.BidBondApply;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.model.mkt.ProjectInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/5/2 20:42
 * @Version 1.0
 */
@Service
public class BidSecurityServiceImpl extends GenericFlowController implements BidSecurityService {
    @Autowired
    private BidSecurityDtoDao bidSecurityDtoDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;
    @Autowired
    private CollectProjectServiceImpl collectProjectService;

    @Override
    public BidSecurityDto queryDetailRecycle(String proCode) {
        BidSecurityDto bidSecurityDto = new BidSecurityDto();
        //查询基本信息
        ProjectInfo projectInfo = bidSecurityDtoDao.findByProCode(proCode);//id是code
        //基本信息
        bidSecurityDto.setProjectInfo(projectInfo);
        //保证金回收信息

        //如果保证金方回收方式=其他 就去查询其他回收方式


        //如果保证金方回收方式=现金 就去查询财务信息 ：收款单位 银行账户 实际付款金额 实际付款日期 评审意见
            //如果是修改 当前登录人的角色的财务人员 就返回一个标记给前端  可以修改实际付款日期



        //附件查询
        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE44.getCode(), null);
        bidSecurityDto.setFileAtt(collectProjectService.fileAttaListToArray(projectAttaList));
        return bidSecurityDto;
    }

    @Override
    public String isFlag(String proCode) {
        return    bidSecurityDtoDao.isFlag(proCode);  //查询发起流程的状态
    }

    @Override
    public BidBondApply selectByProCode(String proCode, String bondType) {
        return  bidSecurityDtoDao.selectByProCode(proCode,bondType);  //查询ID
    }

    @Override
    public BidSecurityDto queryDetail(String proCode,String bondType) {

        BidSecurityDto bidSecurityDto = new BidSecurityDto();
        //查询基本信息
        ProjectInfo projectInfo = bidSecurityDtoDao.findByProCode(proCode);//id是code
        //基本信息
        bidSecurityDto.setProjectInfo(projectInfo);
        //支付申请信息
        BidBondApply bidBondApply = bidSecurityDtoDao.bidBondApplyByProCode(proCode,bondType);
        bidSecurityDto.setBidBondApply(bidBondApply);
        //附件信息
        //附件查询
        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE23.getCode(), null);
        bidSecurityDto.setFileAtt(collectProjectService.fileAttaListToArray(projectAttaList));
        return bidSecurityDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(BidSecurityDto bidSecurityDto, String code) {
        //评审意见
        bidSecurityDtoDao.updateBidBondApply(bidSecurityDto.getBidBondApply().getReviewMemo(), code,bidSecurityDto.getBidBondApply().getBondType());
        //投标日期
        Date bidDate = bidSecurityDto.getBidBondApply().getBidDate();
        if (bidDate != null){
            bidSecurityDtoDao.updateBidInfo( bidDate,code);
        }

        //附件保存
        //获取用户
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //前端附件信息
        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if (bidSecurityDto.getFileAtt() != null) {
            String[] fileAtt = bidSecurityDto.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, code, file);
                attachmentList.add(attachment);
            }
        }
        //更新附件信息
        projectAttachmentDao.deleteByWhere(code, ProjectFileTypeEnum.FILE_TYPE23.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }
        return 1;
    }


}
